package com.higold.cloud.ui.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.higold.cloud.R
import com.higold.cloud.base.BaseFragment
import com.higold.cloud.mvp.contract.AchieveContract
import com.higold.cloud.mvp.model.bean.AchieveBean
import com.higold.cloud.mvp.model.bean.Data
import com.higold.cloud.mvp.presenter.AchievePresenter
import com.higold.cloud.net.exception.ErrorStatus
import com.higold.cloud.showToast
import com.higold.cloud.ui.adapter.NewAgentAdapter
import kotlinx.android.synthetic.main.fragment_agent_list.*
import java.util.*
import kotlin.collections.ArrayList

/**
 *    author : lxz
 *    date   : 2019/10/22 10:39
 *    desc   :
 */

class NewAgentFragment : BaseFragment(), AchieveContract.View {

    private val mPresenter by lazy { AchievePresenter() }
    private var mTitle: String? = null
    private lateinit var mAdapter: NewAgentAdapter
    private lateinit var list: ArrayList<MultiItemEntity>
    private var isFirst: Boolean = true
    private var isShow: Boolean = true
    private lateinit var mList : List<Data>

    companion object {
        fun getInstance(title: String): NewAgentFragment {
            val fragment = NewAgentFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    init {
        mPresenter.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.fragment_agent_list

    override fun lazyLoad() {
        mPresenter.getAchieveInfo(0)
    }

    override fun setAchieveInfo(achieveBean: AchieveBean) {
        multipleStatusView.showContent()

        rv_agent.setHasFixedSize(true)
        rv_agent.layoutManager = LinearLayoutManager(activity)
        mList = achieveBean.data.reversed()
        mAdapter = NewAgentAdapter(mList as ArrayList<Data>)
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM)

        rv_agent.adapter = mAdapter
        mAdapter.isFirstOnly(false)//init firstOnly state
    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView.showNoNetwork()
        } else {
            multipleStatusView.showError()
        }
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {

    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

    override fun initView() {

        tv_sales_account.setOnClickListener {
            mPresenter.getAchieveInfo(0)
            tv_sales_account.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            tv_achieve_rate.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }

        tv_achieve_rate.setOnClickListener {
            mPresenter.getAchieveInfo(1)
            tv_achieve_rate.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            tv_sales_account.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        }

    }

}